﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_Flow_DeleteDocument]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_Flow_DeleteDocument];
GO
CREATE PROCEDURE [dbo].[sproc_Flow_DeleteDocument]
    @DocID int

AS
SET NOCOUNT ON
IF EXISTS(SELECT 1 FROM uds_flow_document 
    WHERE doc_id = @DocID)
BEGIN
    --删除表单
    DELETE FROM uds_flow_Style_Data
        WHERE doc_id = @DocID
    --删除文档信息
    DELETE FROM  uds_flow_document 
        WHERE doc_id = @DocID
    --删除附件信息
    DELETE FROM  uds_flow_files
        WHERE docid = @DocID
    --删除签收
    DELETE FROM uds_flow_status 
        WHERE doc_id = @DocID

    --删除批注的附件
    DELETE FROM  uds_flow_files
        WHERE fileid in (
                    SELECT [file_id] 
                        FROM 
                            uds_flow_postil 
                        WHERE     doc_id = @DocID
                    )
    --删除真正的消息
    DELETE FROM UDS_SMS_MobileMsgSendBuffer        
        WHERE     msgid in (
                    SELECT     msgid 
                        FROM 
                            uds_flow_message
                        WHERE     doc_id = @docid
                  )

    --删除当前步骤的提醒
    DELETE FROM uds_flow_message
        WHERE     doc_id = @docid

    --删除批注
    DELETE FROM uds_flow_postil 
        WHERE doc_id = @DocID

    --删除路径备份
    DELETE FROM uds_flow_path 
        WHERE doc_id = @DocID

    
    PRINT '文档删除成功！'
    RETURN 0
END 
ELSE
BEGIN
    PRINT '文档不存在！'
    RETURN -1
END


SET NOCOUNT OFF